package fr.kirin.logger.model.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import fr.kirin.logger.i18n.I18N;
import fr.kirin.logger.model.DefaultLevel;
import fr.kirin.logger.model.Level;

public class LevelManager {


	private DefaultListModel lvlModel;
	private JPanel main;
	private JDialog informationBox;
	
	public LevelManager(JFrame parent){
		main = new JPanel(null);
		BoxLayout layout = new BoxLayout(main,BoxLayout.LINE_AXIS);
		main.setLayout(layout);
		main.setBorder(BorderFactory.createTitledBorder(I18N.getMessage("lvlConfig")));
		
		
		
		lvlModel = new DefaultListModel();
		for(Level l : DefaultLevel.values()){
			lvlModel.addElement(l);
		}
		
		final JList lvlList = new JList(lvlModel);
		lvlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lvlList.setCellRenderer(new DefaultListCellRenderer() {
		
			@Override
			public Component getListCellRendererComponent(JList list, Object value,
					int index, boolean isSelected, boolean cellHasFocus) {
				 super.getListCellRendererComponent(list, value, index,
							isSelected, cellHasFocus);
				Level level = (Level) value;
				setForeground(level.getColor());
				 return this;
			}
		
		});
		JScrollPane scroll = new JScrollPane(lvlList);
		Dimension listDim = new Dimension(110,150);
		scroll.setPreferredSize(listDim);
		scroll.setMaximumSize(listDim);
		
		main.add(Box.createHorizontalStrut(5));
		main.add(scroll);
		main.add(Box.createHorizontalStrut(5));
		
		Box buttons = Box.createVerticalBox();
		JButton add = new JButton(I18N.getMessage("add"));
		buildInformationBox(parent,add);
		
		
		JButton remove = new JButton(I18N.getMessage("remove"));
		remove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedIndex = lvlList.getSelectedIndex();
				if(selectedIndex != -1)
					lvlModel.remove(selectedIndex);
			}
		
		});
		JButton edit = new JButton(I18N.getMessage("edit"));
		
		buttons.add(add);
		buttons.add(Box.createVerticalStrut(5));
		buttons.add(remove);
		buttons.add(Box.createVerticalStrut(5));
		buttons.add(edit);
		
		
		main.add(buttons);
		main.add(Box.createHorizontalStrut(5));
		
	}
	
	private void buildInformationBox(JFrame parent, JButton add){
		informationBox = new JDialog(parent,true);
	
		add.addActionListener(new ActionListener() {
		
			public void actionPerformed(ActionEvent e) {
				informationBox.setVisible(true);
			}
		
		});
	}
	
	
	public JPanel getUIComponent(){
		return main;
	}
	
	
}
